package com.redsoil.house.ui.remit.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redsoil.house.R;
import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.BaseActivity;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.model.bean.ReceiptListVO;
import com.redsoil.house.model.bean.ReceiptTotalVO;
import com.redsoil.house.model.bean.TestVO;
import com.redsoil.house.ui.news.activity.NewsInfoActivity;
import com.redsoil.house.ui.news.adapter.NewsAdapter;
import com.redsoil.house.ui.remit.adapter.RemitAdapter;
import com.redsoil.house.ui.remit.contract.RemitContract;
import com.redsoil.house.ui.remit.presenter.RemitPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 汇款履历
 */
public class RemitActivity extends RootActivity<RemitPresenter> implements RemitContract.View,XBaseTopBarImp{

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    @BindView(R.id.previous_year_tv)
    TextView previous_year_tv;

    @BindView(R.id.next_year_tv)
    TextView next_year_tv;

    @BindView(R.id.year_tv)
    TextView year_tv;

    @BindView(R.id.total_tv)
    TextView total_tv;

    @BindView(R.id.targetTotal_tv)
    TextView targetTotal_tv;

    private RemitAdapter remitAdapter;

    private int current_year ;

    @Override
    protected int getLayout() {
        return R.layout.activity_remit;
    }

    @Override
    protected void initEventAndData() {

        super.initEventAndData();

        Calendar cd = Calendar.getInstance();
        current_year = cd.get(Calendar.YEAR);

        stateLoading();

        main_topbar.setxBaseTopBarImp(this);

        sendReceipt();
    }

    @OnClick(R.id.previous_year_tv)
    void onPreviousYear(){
        stateLoading();
        current_year--;
        sendReceipt();
    }

    @OnClick(R.id.next_year_tv)
    void onNextYear(){
        stateLoading();
        current_year++;
        sendReceipt();
    }

    private void sendReceipt(){
        year_tv.setText(current_year+"年");
//        main_topbar.setMiddleText(current_year+"");
        mPresenter.postReceipt(current_year+"");
    }

    @Override
    public void responeError(String errorMsg) {
        stateError();
    }

    @Override
    public void postReceiptListSuccess(ReceiptListVO receiptListVO) {

        if(remitAdapter == null){

            //添加自定义分割线
            DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
            divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.remit_list_divider));
            rv_list.addItemDecoration(divider);

            rv_list.setLayoutManager(new LinearLayoutManager(this));


            remitAdapter = new RemitAdapter(R.layout.remit_list_item,receiptListVO.getDataList());

            remitAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                }
            });

            rv_list.setAdapter(remitAdapter);
        }else {
            remitAdapter.setNewData(receiptListVO.getDataList());
        }

    }

    @Override
    public void postReceiptTotalSuccess(ReceiptTotalVO receiptTotalVO) {
        total_tv.setText(receiptTotalVO.getTotal());
        if(AppContext.getInstance().getCurrentMoneyType().equals("JPY")){
            targetTotal_tv.setText("");
        }else {
            targetTotal_tv.setText("("+ receiptTotalVO.getTargetTotalSymbol()+receiptTotalVO.getTargetTotal()+")");
        }

    }

    @Override
    public void postComplete() {
        stateMain();
    }

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {}

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, RemitActivity.class);
        return intent;
    }

}
